﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class GuidedMortgage : Form
    {
        public GuidedMortgage()
        {
            InitializeComponent();
        }

        private void GuidedMortgage_Load(object sender, EventArgs e)
        {
            ddlFeederFund.DataSource = MainForm.projector.GetFundsOfClass(FundClassEnum.SpendingAccount);

            dtpOriginationDate.Value = DateTime.Today.AddMonths(-6);

            if (ddlFeederFund.Items.Count == 0)
            {
                MessageBox.Show("You will not be able to add a mortgage without a checking/spending account", "Unable to add mortgage", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private bool ValidateForm()
        {
            string ErrorList = "";

            txtLoanName.Text = txtLoanName.Text.Trim();
            if (txtLoanName.Text == "")
            {
                ErrorList += "- You must enter a valid Loan Name\n";
            }
            else
            {
                if (MainForm.projector.FindFund(txtLoanName.Text) != null)
                    ErrorList += "- A Fund already exists by that name\n";
            }

            if (ddlFeederFund.Items.Count == 0)
            {
                ErrorList += "- You first need to add a checking/spending account to make payments from.\n";
                return false;
            }

            try
            {
                Decimal la = Decimal.Parse(txtLoanAmount.Text);
                if (la < 0.0m)
                {
                    ErrorList += "- Loan amount must be greater than zero\n";
                }
            }
            catch 
            {
                ErrorList += "- Loan amount is not a valid number\n";
            }

            try { Decimal eo = Decimal.Parse(txtDownpayment.Text); }
            catch
            {
                ErrorList += "- Downpayment is not a valid number\n";
            }

            if (ErrorList == "") return true;

            MessageBox.Show("Please correct the following errors :\n" + ErrorList, "Unable to add Mortgage", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;

        }

        #region Button Handlers

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm()) return;

            Decimal InceptionAmount = 0.0m, InterestRate = 0.0m, PaymentAmount = 0.0m;
            Decimal EquityOffset = 0.0m;

            InceptionAmount = Decimal.Parse(txtLoanAmount.Text);
            InterestRate = Decimal.Parse(txtInterestRate.Text);
            PaymentAmount = Decimal.Parse(txtLoanPayment.Text);
            EquityOffset = Decimal.Parse(txtDownpayment.Text);

            Fund fund = new Fund(
                new Balance(InceptionAmount, dtpOriginationDate.Value, "Loan Inception"),
                txtLoanName.Text, FundClassEnum.DebtEquity, false);

            fund.EquityOffset = EquityOffset;

            Actor actor = new Actor(txtLoanName.Text + " LoanPayment", txtLoanName.Text, ddlFeederFund.Text,
                ActorPeriodicityEnum.Monthly, 1, dtpOriginationDate.Value, PaymentAmount);

            actor.IsLoanPayment = true;
            actor.InterestRate = InterestRate; 
            actor.LoanDurationInYears = (int) numLoanDuration.Value;

            MainForm.projector.Funds.Add(fund);
            MainForm.projector.Actors.Add(actor);

            MainForm.InstanceRef.HasChanged = true;

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region Simple Numeric Textbox Filtering

        private void txtLoanAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        private void txtInterestRate_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        private void txtLoanPayment_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        private void txtDownpayment_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        #endregion

        private void lnkOnlineHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "http://project-continuum.com/OnlineHelp/GuidedMortgage.aspx";
            proc.Start();
        }

    }
}
